package com.hnkywl.sts.dao.report;

import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import oracle.jdbc.OracleCallableStatement;
import org.hibernate.SQLQuery;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-14
 * Time: 下午4:51
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class WlycwsfcDao extends SimpleHibernateTemplate {

    /**
     * 财务收发存报表
     *
     * @param gsid
     * @param year
     * @param month
     * @return
     */
    public Map<String, List> wlyCwsfcList(Long gsid, Integer year, Integer month) {
        Integer time;
        if(month<10){
             time = Integer.parseInt(year.toString() +"0"+ month.toString());
        }else {
             time = Integer.parseInt(year.toString() + month.toString());
        }
        if (gsid == null)
            gsid = -1L;
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call RPT_CW_SFC(?,?,?)}");
            call.setLong(1, gsid);
            call.setInt(2, time);
            call.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);
            call.execute();
            //====================================
            rs = ((OracleCallableStatement) call).getCursor(3);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, List> result = new HashMap<String, List>();
            List<Map> rows = new ArrayList<Map>();
            List<Map> footer = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                if ("1".equals(r.get("flag").toString()))
                    rows.add(r);
                else
                    footer.add(r);
            }
            result.put("rows", rows);
            result.put("footer", footer);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }


    private void closeResource(ResultSet rs, Statement s, Connection con) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (s != null)
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (con != null)
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
    }


    public String cbjz(Integer year, Integer month) {
        Integer time;
        if(month<10){
            time = Integer.parseInt(year.toString() +"0"+ month.toString());
        }else {
            time = Integer.parseInt(year.toString() + month.toString());
        }
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call rpt_cw_sfc_xscbjz(?,?)}");
            call.setInt(1, time);
            call.registerOutParameter(2, Types.VARCHAR);
            call.execute();
            //====================================
            String errorMsg = call.getString(2);
            return errorMsg;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }



}
